package com.example.administrator.ksf_zty.MaterialActivity;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;

import com.example.administrator.ksf_zty.JavaModels.SysUserModel;
import com.example.administrator.ksf_zty.R;
import com.example.administrator.ksf_zty.util.BaseActivity;
import com.example.administrator.ksf_zty.util.HttpUtil;
import com.example.administrator.ksf_zty.util.Tips;
import com.example.administrator.ksf_zty.util.TitleUtils;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.RequestBody;
import okhttp3.Response;

import static com.example.administrator.ksf_zty.util.HttpUtils.StrHttp_URLZ_TRCE_H_DOWNLOAD_GRDOCUMENT_PDA;

public class MaterialSupplierBackActivity extends BaseActivity {

    ImageButton back;
    Button titleButton;
    EditText txtBarcode;
    EditText txtDoucno;
    EditText txtQty;
    EditText txtName;
    EditText txtPC;
    Spinner ddlType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_material_supplier_back);
        txtBarcode = (EditText) findViewById(R.id.MaterialSupplierBack_txtBarcode);
        txtDoucno = (EditText) findViewById(R.id.MaterialSupplierBack_txtDoucno);
        txtQty = (EditText) findViewById(R.id.MaterialSupplierBack_txtQty);
        txtName = (EditText) findViewById(R.id.MaterialSupplierBack_txtName);
        txtPC = (EditText) findViewById(R.id.MaterialSupplierBack_txtPC);
        ddlType = (Spinner) findViewById(R.id.MaterialSupplierBack_ddlType);
        SetTitle();
        SetOnClick();
        SetEditTextClick();
    }


    //给按钮设置事件
    private void SetOnClick() {
        //保存按钮
        Button btnSave = (Button) findViewById(R.id.MaterialSupplierBack_btnSave);
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SetbtnSave();
            }
        });
        //查询详情按钮
        Button btnSel = (Button) findViewById(R.id.MaterialSupplierBack_btnSel);
        btnSel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MaterialSupplierBackActivity.this, MaterialSupplierBackDetailsActivity.class);
                intent.putExtra("name", "");//传参数
                startActivity(intent);
            }
        });
        //提交按钮
        Button btnSubmit = (Button) findViewById(R.id.MaterialSupplierBack_btnSubmit);
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SetbtnSubmit();
            }
        });
        //退回供应商
        Button btnCancel = (Button) findViewById(R.id.MaterialSupplierBack_btnCancel);
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MaterialSupplierBackActivity.this, MaterialMenuActivity.class);
                intent.putExtra("name", "");//传参数
                startActivity(intent);
            }
        });
    }

    //给文本框回车时间
    private void SetEditTextClick() {
        //输入单号，监听回车键
        txtDoucno.setOnKeyListener(new View.OnKeyListener() {
            @TargetApi(Build.VERSION_CODES.CUPCAKE)
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    Log.d("lhd", "按下的按键为：" + keyCode);
                    //隐藏软键盘
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                    String strDoucno = (txtDoucno.getText() + "").replace(" ", "").toUpperCase();//去空格
                    txtDoucno.setText(strDoucno);
                    GetDoucno(strDoucno);
                    return true;
                }
                return false;
            }
        });

        //输入栈板条码监听回车键
        txtBarcode.setOnKeyListener(new View.OnKeyListener() {
            @TargetApi(Build.VERSION_CODES.CUPCAKE)
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    Log.d("lhd", "按下的按键为：" + keyCode);
                    //隐藏软键盘
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                    String strBarcode = (txtBarcode.getText() + "").replace(" ", "").toUpperCase();//去空格
                    txtBarcode.setText(strBarcode);
                    GetBarCode(strBarcode);
                    return true;
                }
                return false;
            }
        });
        //输入数量监听回车键
        txtQty.setOnKeyListener(new View.OnKeyListener() {
            @TargetApi(Build.VERSION_CODES.CUPCAKE)
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    Log.d("lhd", "按下的按键为：" + keyCode);
                    //隐藏软键盘
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                    String strQty = (txtQty.getText() + "").replace(" ", "").toUpperCase();//去空格
                    txtQty.setText(strQty);
                    if (strQty.isEmpty()) {

                    }
                    SetbtnSave();
                    return true;
                }
                return false;
            }
        });
    }

    //下载SAP单据
    private void GetDoucno(String text) {
        String strType = ddlType.toString();
        String strI_BWART = "";
        if (strType.equals("退货")) {
            strI_BWART = "122";
        } else if (strType.equals("判退")) {
            strI_BWART = "161";
        }
        if (strI_BWART.isEmpty()) {
            Tips.show(MaterialSupplierBackActivity.this, "请选择退库类型", 1000);
            return;
        }
        if ("".equals(text.trim())) {
            Tips.show(MaterialSupplierBackActivity.this, "单号不能为空", 1000);
            return;
        }
        //原物料退回供应商接口配置
        // StrHttp_URLZ_TRCE_H_DOWNLOAD_GRDOCUMENT_PDA=mainUrl + "Z_TRCE_H_DOWNLOAD_GRDOCUMENT_PDA";//退回供应商单据下载接口

        FormBody.Builder builder = new FormBody.Builder();
        builder.add("I_WERKS", "ZTY1");
        builder.add("I_MBLNR", text);
        builder.add("I_BWART", strI_BWART);
        RequestBody formBody = builder.build();
        HttpUtil.sendOkHttpRequest(StrHttp_URLZ_TRCE_H_DOWNLOAD_GRDOCUMENT_PDA, formBody, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("LOG111", "网络异常11");
                MaterialSupplierBackActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //这里的代码在主线程中执行
                        Tips.show(MaterialSupplierBackActivity.this, "网络异常11", 1000);
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.d("LOG111", "成功");
                //解析数据
                final String responseData = response.body().string();
                try {
                    final JSONObject obj = new JSONObject(responseData);
                    if (obj.getString("success").equals("0")) {
                        //给集合对象里面赋值
                        SysUserModel sysUserModel = new Gson().fromJson(responseData, SysUserModel.class);
                        final String strhttpPassWord = sysUserModel.getList().get(0).getPassWord();


                    } else {
                        //弹出提示框
                        MaterialSupplierBackActivity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                //这里的代码在主线程中执行
                                Tips.show(MaterialSupplierBackActivity.this, "用户不存在", 1000);
                            }
                        });
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });





        //点击回车键后出现粘贴BUG，这里把焦点移动到数量上
        txtBarcode.setFocusable(true);
        txtBarcode.setFocusableInTouchMode(true);
        txtBarcode.requestFocus();
        txtBarcode.requestFocusFromTouch();

    }

    //根据栈板条码获取栈板信息
    private void GetBarCode(String text) {
        //为解决点击回车键后出现粘贴BUG，这里把焦点移动到数量上
        txtQty.setFocusable(true);
        txtQty.setFocusableInTouchMode(true);
        txtQty.requestFocus();
        txtQty.requestFocusFromTouch();

        if ("".equals(text.trim())) {
            Tips.show(MaterialSupplierBackActivity.this, "单号不能为空", 1000);
            return;
        }
        Tips.show(MaterialSupplierBackActivity.this, text.trim(), 1000);
        //StrHttp_URLGetZBBarCode=mainUrl + "GetZBBarCode";//或者栈板条码数据
    }

    //保存按钮
    private void SetbtnSave() {
        //将数据保存在本地数据库中
        Tips.show(MaterialSupplierBackActivity.this, "保存成功", 1000);
    }

    //提交按钮
    private void SetbtnSubmit() {
        //StrHttp_URLZ_TRCE_H_UPLOAD_GRDOCUMENT_PDA=mainUrl + "Z_TRCE_H_UPLOAD_GRDOCUMENT_PDA";//上传退回供应商单据
    }


    //设置Title
    private void SetTitle() {
        back = (ImageButton) findViewById(R.id.back);
        titleButton = (Button) findViewById(R.id.titleButton);
        //设置标题(第三个参数是标题文字，直接替换""内容即可，第五个参数是标题右边按钮文字，第六个参数是设置是否显示返回按钮)
        TitleUtils.setTitle(this, R.id.title_sys_set, "退回供应商", this,
                "", true);
        //返回按钮事件
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }
}
